package com.eastedu.util;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.eastedu.common.model.question.Fragment;
import com.eastedu.common.model.question.QuestionItemContent;
import com.eastedu.repository.question.dto.QuestionItemDTO;
import com.eastedu.repository.question.po.QuestionItemPO;
import com.eastedu.utils.JsonUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;
import java.util.stream.Collectors;

/**
 * The type Question util.
 *
 * @author ZHISHUAI.ZHANG
 */
public class QuestionUtil {

    /**
     * Gets question content string.
     *
     * @param questionItemJson the question item json
     * @return the question content string
     */
    public static String extractContent(String questionItemJson) {
        List<QuestionItemPO> questionItems = JSON.parseArray(questionItemJson, QuestionItemPO.class);
        if (CollectionUtils.isEmpty(questionItems)) {
            return null;
        }
        List<String> questionContents = questionItems.stream().filter(Objects::nonNull).map(QuestionItemPO::getContent)
                .filter(StringUtils::isNotBlank)
                .map(content -> {
                    List<QuestionItemContent> contents = JSON.parseArray(content, QuestionItemContent.class);
                    if (CollectionUtils.isEmpty(contents)) {
                        return new ArrayList<String>();
                    }
                    return contents.stream().filter(Objects::nonNull).map(QuestionItemContent::getFragments)
                            .filter(CollectionUtils::isNotEmpty).map(fragments -> fragments.stream()
                                    .filter(Objects::nonNull)
                                    .map(Fragment::getContent).filter(StringUtils::isNotBlank)
                                    .collect(Collectors.toList()))
                            .flatMap(Collection::stream).filter(StringUtils::isNotBlank)
                            .collect(Collectors.toList());
                }).filter(CollectionUtils::isNotEmpty).flatMap(Collection::stream).collect(Collectors.toList());
        if (CollectionUtils.isEmpty(questionContents)) {
            return null;
        }
        return questionContents.stream().filter(StringUtils::isNotBlank).collect(Collectors.joining());
    }


    /**
     * 将试题题干转换为嵌套结构
     * 不需要objectiveAnswer
     *
     * @param pos the pos
     * @param parentId the parent id
     * @return list list
     */
    public static List<QuestionItemDTO> nestedSubItem(List<QuestionItemPO> pos, Long parentId) {
        List<QuestionItemDTO> items = new ArrayList<>();
        if (pos == null) {
            return items;
        }
        List<QuestionItemPO> questions = pos.stream().filter(item -> item.getParentId().compareTo(parentId) == 0).toList();
        for (QuestionItemPO po : questions) {
            QuestionItemDTO dto = new QuestionItemDTO();
            dto.setId(po.getId());
            dto.setQuestionId(po.getQuestionId());
            dto.setAnswer(json2Object(po.getAnswer(), QuestionItemContent.class));
            dto.setAnswerPosition(json2Object(po.getAnswerPosition(), QuestionItemContent.class));
            dto.setQuestionType(po.getQuestionType());
            dto.setExplanation(json2Object(po.getExplanation(), QuestionItemContent.class));
            dto.setOption(json2Object(po.getOptions(), QuestionItemContent.class));
            dto.setStem(json2Object(po.getContent(), QuestionItemContent.class));
            dto.setQuestions(nestedSubItem(pos, po.getId()));


            dto.setObjectiveAnswer(po.getObjectiveAnswer());
            dto.setObjectiveOption(po.getObjectiveOption());
            dto.setContentText(po.getContentText());
            dto.setExplanationText(po.getExplanationText());
            dto.setAnswerText(po.getAnswerText());
            dto.setContentImages(po.getContentImages());
            dto.setAnswerImages(po.getAnswerImages());
            dto.setExplanationImages(po.getExplanationImages());
            dto.setAnswerPositionImages(po.getAnswerPositionImages());
            dto.setLogicalParagraph(po.getLogicalParagraph());
            dto.setAnswerImagePath(po.getAnswerImagePath());
            dto.setExplanationImagePath(po.getExplanationImagePath());
            dto.setOptionImagePath(po.getOptionImagePath());
            dto.setStemImagePath(po.getStemImagePath());
            dto.setObjectiveOptionCount(po.getObjectiveOptionCount());
            dto.setOrder(po.getOrder());
            dto.setState(po.getState());

            items.add(dto);
        }
        return items;
    }

    /**
     * json字符串转换为object
     *
     * @param <T>        the type parameter
     * @param json the json
     * @param clazz the clazz
     * @return list list
     */
    public static <T> List<T> json2Object(String json, Class<T> clazz) {
        if (StringUtils.isBlank(json)) {
            return Collections.emptyList();
        }
        return JSONObject.parseArray(json, clazz);
    }


    /**
     * 将题干信息扁平化处理
     *
     * @param item the item
     * @param parentId the parent id
     * @param pos the pos
     */
    public static void flatSubItem(QuestionItemDTO item, Long parentId, List<QuestionItemPO> pos) {
        if (item != null) {
            QuestionItemPO po = new QuestionItemPO();


            po.setId(item.getId());
            po.setQuestionId(item.getQuestionId());
            po.setParentId(parentId);
            po.setExplanation(JsonUtil.toJsonString(item.getExplanation()));
            po.setQuestionType(item.getQuestionType());
            po.setAnswer(JsonUtil.toJsonString(item.getAnswer()));
            po.setContent(JsonUtil.toJsonString(item.getStem()));
            po.setOptions(JsonUtil.toJsonString(item.getOption()));
            po.setAnswerPosition(JsonUtil.toJsonString(item.getAnswerPosition()));

            po.setState(item.getState());
            po.setObjectiveAnswer(item.getObjectiveAnswer());
            po.setObjectiveOption(item.getObjectiveOption());
            po.setContentText(item.getContentText());
            po.setExplanationText(item.getExplanationText());
            po.setAnswerText(item.getAnswerText());
            po.setContentImages(item.getContentImages());
            po.setAnswerImages(item.getAnswerImages());
            po.setExplanationImages(item.getExplanationImages());
            po.setAnswerPositionImages(item.getAnswerPositionImages());
            po.setLogicalParagraph(item.getLogicalParagraph());
            po.setAnswerImagePath(item.getAnswerImagePath());
            po.setExplanationImagePath(item.getExplanationImagePath());
            po.setOptionImagePath(item.getOptionImagePath());
            po.setStemImagePath(item.getStemImagePath());
            po.setObjectiveOptionCount(item.getObjectiveOptionCount());
            po.setOrder(item.getOrder());

            List<QuestionItemDTO> questions = item.getQuestions();
            if (!org.springframework.util.CollectionUtils.isEmpty(questions)) {
                for (QuestionItemDTO question : questions) {
                    flatSubItem(question, po.getId(), pos);
                }
            }
            pos.add(po);
        }
    }
}
